home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / mac / app / threading.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-07-22  |  34.9 KB  |  838 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. """Thread module emulating a subset of Java's threading model."""
  5. import sys as _sys
  6.  
  7. try:
  8.     import thread
  9. except ImportError:
  10.     del _sys.modules[__name__]
  11.     raise 
  12.  
  13. from StringIO import StringIO as _StringIO
  14. from time import time as _time, sleep as _sleep
  15. from traceback import print_exc as _print_exc
  16. __all__ = [
  17.     'activeCount',
  18.     'Condition',
  19.     'currentThread',
  20.     'enumerate',
  21.     'Event',
  22.     'Lock',
  23.     'RLock',
  24.     'Semaphore',
  25.     'BoundedSemaphore',
  26.     'Thread',
  27.     'Timer',
  28.     'setprofile',
  29.     'settrace']
  30. _start_new_thread = thread.start_new_thread
  31. _allocate_lock = thread.allocate_lock
  32. _get_ident = thread.get_ident
  33. ThreadError = thread.error
  34. del thread
  35. _VERBOSE = False
  36. if __debug__:
  37.     
  38.     class _Verbose(object):
  39.         
  40.         def __init__(self, verbose = None):
  41.             if verbose is None:
  42.                 verbose = _VERBOSE
  43.             
  44.             self._Verbose__verbose = verbose
  45.  
  46.         
  47.         def _note(self, format, *args):
  48.             if self._Verbose__verbose:
  49.                 format = format % args
  50.                 format = '%s: %s\n' % (currentThread().getName(), format)
  51.                 _sys.stderr.write(format)
  52.             
  53.  
  54.  
  55. else:
  56.     
  57.     class _Verbose(object):
  58.         
  59.         def __init__(self, verbose = None):
  60.             pass
  61.  
  62.         
  63.         def _note(self, *args):
  64.             pass
  65.  
  66.  
  67. _profile_hook = None
  68. _trace_hook = None
  69.  
  70. def setprofile(func):
  71.     global _profile_hook
  72.     _profile_hook = func
  73.  
  74.  
  75. def settrace(func):
  76.     global _trace_hook
  77.     _trace_hook = func
  78.  
  79. Lock = _allocate_lock
  80.  
  81. def RLock(*args, **kwargs):
  82.     return _RLock(*args, **kwargs)
  83.  
  84.  
  85. class _RLock(_Verbose):
  86.     
  87.     def __init__(self, verbose = None):
  88.         _Verbose.__init__(self, verbose)
  89.         self._RLock__block = _allocate_lock()
  90.         self._RLock__owner = None
  91.         self._RLock__count = 0
  92.  
  93.     
  94.     def __repr__(self):
  95.         if self._RLock__owner:
  96.             pass
  97.         return '<%s(%s, %d)>' % (self.__class__.__name__, self._RLock__owner.getName(), self._RLock__count)
  98.  
  99.     
  100.     def acquire(self, blocking = 1):
  101.         me = currentThread()
  102.         if self._RLock__owner is me:
  103.             self._RLock__count = self._RLock__count + 1
  104.             if __debug__:
  105.                 self._note('%s.acquire(%s): recursive success', self, blocking)
  106.             
  107.             return 1
  108.         
  109.         rc = self._RLock__block.acquire(blocking)
  110.         if rc:
  111.             self._RLock__owner = me
  112.             self._RLock__count = 1
  113.             if __debug__:
  114.                 self._note('%s.acquire(%s): initial succes', self, blocking)
  115.             
  116.         elif __debug__:
  117.             self._note('%s.acquire(%s): failure', self, blocking)
  118.         
  119.         return rc
  120.  
  121.     
  122.     def release(self):
  123.         me = currentThread()
  124.         if not self._RLock__owner is me:
  125.             raise AssertionError, 'release() of un-acquire()d lock'
  126.         self._RLock__count = count = self._RLock__count - 1
  127.         if not count:
  128.             self._RLock__owner = None
  129.             self._RLock__block.release()
  130.             if __debug__:
  131.                 self._note('%s.release(): final release', self)
  132.             
  133.         elif __debug__:
  134.             self._note('%s.release(): non-final release', self)
  135.         
  136.  
  137.     
  138.     def _acquire_restore(self, .2):
  139.         (count, owner) = .2
  140.         self._RLock__block.acquire()
  141.         self._RLock__count = count
  142.         self._RLock__owner = owner
  143.         if __debug__:
  144.             self._note('%s._acquire_restore()', self)
  145.         
  146.  
  147.     
  148.     def _release_save(self):
  149.         if __debug__:
  150.             self._note('%s._release_save()', self)
  151.         
  152.         count = self._RLock__count
  153.         self._RLock__count = 0
  154.         owner = self._RLock__owner
  155.         self._RLock__owner = None
  156.         self._RLock__block.release()
  157.         return (count, owner)
  158.  
  159.     
  160.     def _is_owned(self):
  161.         return self._RLock__owner is currentThread()
  162.  
  163.  
  164.  
  165. def Condition(*args, **kwargs):
  166.     return _Condition(*args, **kwargs)
  167.  
  168.  
  169. class _Condition(_Verbose):
  170.     
  171.     def __init__(self, lock = None, verbose = None):
  172.         _Verbose.__init__(self, verbose)
  173.         if lock is None:
  174.             lock = RLock()
  175.         
  176.         self._Condition__lock = lock
  177.         self.acquire = lock.acquire
  178.         self.release = lock.release
  179.         
  180.         try:
  181.             self._release_save = lock._release_save
  182.         except AttributeError:
  183.             pass
  184.  
  185.         
  186.         try:
  187.             self._acquire_restore = lock._acquire_restore
  188.         except AttributeError:
  189.             pass
  190.  
  191.         
  192.         try:
  193.             self._is_owned = lock._is_owned
  194.         except AttributeError:
  195.             pass
  196.  
  197.         self._Condition__waiters = []
  198.  
  199.     
  200.     def __repr__(self):
  201.         return '<Condition(%s, %d)>' % (self._Condition__lock, len(self._Condition__waiters))
  202.  
  203.     
  204.     def _release_save(self):
  205.         self._Condition__lock.release()
  206.  
  207.     
  208.     def _acquire_restore(self, x):
  209.         self._Condition__lock.acquire()
  210.  
  211.     
  212.     def _is_owned(self):
  213.         if self._Condition__lock.acquire(0):
  214.             self._Condition__lock.release()
  215.             return False
  216.         else:
  217.             return True
  218.  
  219.     
  220.     def wait(self, timeout = None):
  221.         currentThread()
  222.         if not self._is_owned():
  223.             raise AssertionError, 'wait() of un-acquire()d lock'
  224.         waiter = _allocate_lock()
  225.         waiter.acquire()
  226.         self._Condition__waiters.append(waiter)
  227.         saved_state = self._release_save()
  228.         
  229.         try:
  230.             if timeout is None:
  231.                 waiter.acquire()
  232.                 if __debug__:
  233.                     self._note('%s.wait(): got it', self)
  234.                 
  235.             else:
  236.                 endtime = _time() + timeout
  237.                 delay = 0.00050000000000000001
  238.                 while True:
  239.                     gotit = waiter.acquire(0)
  240.                     if gotit:
  241.                         break
  242.                     
  243.                     remaining = endtime - _time()
  244.                     if remaining <= 0:
  245.                         break
  246.                     
  247.                     delay = min(delay * 2, remaining, 0.050000000000000003)
  248.                     _sleep(delay)
  249.                 if not gotit:
  250.                     if __debug__:
  251.                         self._note('%s.wait(%s): timed out', self, timeout)
  252.                     
  253.                     
  254.                     try:
  255.                         self._Condition__waiters.remove(waiter)
  256.                     except ValueError:
  257.                         pass
  258.                     except:
  259.                         None<EXCEPTION MATCH>ValueError
  260.                     
  261.  
  262.                 None<EXCEPTION MATCH>ValueError
  263.                 if __debug__:
  264.                     self._note('%s.wait(%s): got it', self, timeout)
  265.                 self._acquire_restore(saved_state)
  266.                 return None
  267.  
  268.  
  269.     
  270.     def notify(self, n = 1):
  271.         currentThread()
  272.         if not self._is_owned():
  273.             raise AssertionError, 'notify() of un-acquire()d lock'
  274.         _Condition__waiters = self._Condition__waiters
  275.         waiters = _Condition__waiters[:n]
  276.         if not waiters:
  277.             if __debug__:
  278.                 self._note('%s.notify(): no waiters', self)
  279.             
  280.             return None
  281.         
  282.         if not n != 1 and 's':
  283.             pass
  284.         self._note('%s.notify(): notifying %d waiter%s', self, n, '')
  285.         for waiter in waiters:
  286.             waiter.release()
  287.             
  288.             try:
  289.                 _Condition__waiters.remove(waiter)
  290.             continue
  291.             except ValueError:
  292.                 continue
  293.             
  294.  
  295.         
  296.  
  297.     
  298.     def notifyAll(self):
  299.         self.notify(len(self._Condition__waiters))
  300.  
  301.  
  302.  
  303. def Semaphore(*args, **kwargs):
  304.     return _Semaphore(*args, **kwargs)
  305.  
  306.  
  307. class _Semaphore(_Verbose):
  308.     
  309.     def __init__(self, value = 1, verbose = None):
  310.         if not value >= 0:
  311.             raise AssertionError, 'Semaphore initial value must be >= 0'
  312.         _Verbose.__init__(self, verbose)
  313.         self._Semaphore__cond = Condition(Lock())
  314.         self._Semaphore__value = value
  315.  
  316.     
  317.     def acquire(self, blocking = 1):
  318.         rc = False
  319.         self._Semaphore__cond.acquire()
  320.         while self._Semaphore__value == 0:
  321.             if not blocking:
  322.                 break
  323.             
  324.             if __debug__:
  325.                 self._note('%s.acquire(%s): blocked waiting, value=%s', self, blocking, self._Semaphore__value)
  326.             
  327.             self._Semaphore__cond.wait()
  328.         self._Semaphore__value = self._Semaphore__value - 1
  329.         if __debug__:
  330.             self._note('%s.acquire: success, value=%s', self, self._Semaphore__value)
  331.         
  332.         rc = True
  333.         self._Semaphore__cond.release()
  334.         return rc
  335.  
  336.     
  337.     def release(self):
  338.         self._Semaphore__cond.acquire()
  339.         self._Semaphore__value = self._Semaphore__value + 1
  340.         if __debug__:
  341.             self._note('%s.release: success, value=%s', self, self._Semaphore__value)
  342.         
  343.         self._Semaphore__cond.notify()
  344.         self._Semaphore__cond.release()
  345.  
  346.  
  347.  
  348. def BoundedSemaphore(*args, **kwargs):
  349.     return _BoundedSemaphore(*args, **kwargs)
  350.  
  351.  
  352. class _BoundedSemaphore(_Semaphore):
  353.     '''Semaphore that checks that # releases is <= # acquires'''
  354.     
  355.     def __init__(self, value = 1, verbose = None):
  356.         _Semaphore.__init__(self, value, verbose)
  357.         self._initial_value = value
  358.  
  359.     
  360.     def release(self):
  361.         if self._Semaphore__value >= self._initial_value:
  362.             raise ValueError, 'Semaphore released too many times'
  363.         
  364.         return _Semaphore.release(self)
  365.  
  366.  
  367.  
  368. def Event(*args, **kwargs):
  369.     return _Event(*args, **kwargs)
  370.  
  371.  
  372. class _Event(_Verbose):
  373.     
  374.     def __init__(self, verbose = None):
  375.         _Verbose.__init__(self, verbose)
  376.         self._Event__cond = Condition(Lock())
  377.         self._Event__flag = False
  378.  
  379.     
  380.     def isSet(self):
  381.         return self._Event__flag
  382.  
  383.     
  384.     def set(self):
  385.         self._Event__cond.acquire()
  386.         
  387.         try:
  388.             self._Event__flag = True
  389.             self._Event__cond.notifyAll()
  390.         finally:
  391.             self._Event__cond.release()
  392.  
  393.  
  394.     
  395.     def clear(self):
  396.         self._Event__cond.acquire()
  397.         
  398.         try:
  399.             self._Event__flag = False
  400.         finally:
  401.             self._Event__cond.release()
  402.  
  403.  
  404.     
  405.     def wait(self, timeout = None):
  406.         self._Event__cond.acquire()
  407.         
  408.         try:
  409.             if not (self._Event__flag):
  410.                 self._Event__cond.wait(timeout)
  411.         finally:
  412.             self._Event__cond.release()
  413.  
  414.  
  415.  
  416. _counter = 0
  417.  
  418. def _newname(template = 'Thread-%d'):
  419.     global _counter
  420.     _counter = _counter + 1
  421.     return template % _counter
  422.  
  423. _active_limbo_lock = _allocate_lock()
  424. _active = { }
  425. _limbo = { }
  426.  
  427. class Thread(_Verbose):
  428.     __initialized = False
  429.     
  430.     def __init__(self, group = None, target = None, name = None, args = (), kwargs = { }, verbose = None):
  431.         if not group is None:
  432.             raise AssertionError, 'group argument must be None for now'
  433.         _Verbose.__init__(self, verbose)
  434.         self._Thread__target = target
  435.         if not name:
  436.             pass
  437.         self._Thread__name = str(_newname())
  438.         self._Thread__args = args
  439.         self._Thread__kwargs = kwargs
  440.         self._Thread__daemonic = self._set_daemon()
  441.         self._Thread__started = False
  442.         self._Thread__stopped = False
  443.         self._Thread__block = Condition(Lock())
  444.         self._Thread__initialized = True
  445.  
  446.     
  447.     def _set_daemon(self):
  448.         return currentThread().isDaemon()
  449.  
  450.     
  451.     def __repr__(self):
  452.         if not self._Thread__initialized:
  453.             raise AssertionError, 'Thread.__init__() was not called'
  454.         status = 'initial'
  455.         if self._Thread__started:
  456.             status = 'started'
  457.         
  458.         if self._Thread__stopped:
  459.             status = 'stopped'
  460.         
  461.         if self._Thread__daemonic:
  462.             status = status + ' daemon'
  463.         
  464.         return '<%s(%s, %s)>' % (self.__class__.__name__, self._Thread__name, status)
  465.  
  466.     
  467.     def start(self):
  468.         if not self._Thread__initialized:
  469.             raise AssertionError, 'Thread.__init__() not called'
  470.         if not not (self._Thread__started):
  471.             raise AssertionError, 'thread already started'
  472.         if __debug__:
  473.             self._note('%s.start(): starting thread', self)
  474.         
  475.         _active_limbo_lock.acquire()
  476.         _limbo[self] = self
  477.         _active_limbo_lock.release()
  478.         _start_new_thread(self._Thread__bootstrap, ())
  479.         self._Thread__started = True
  480.         _sleep(9.9999999999999995e-07)
  481.  
  482.     
  483.     def run(self):
  484.         if self._Thread__target:
  485.             self._Thread__target(*self._Thread__args, **self._Thread__kwargs)
  486.         
  487.  
  488.     
  489.     def __bootstrap(self):
  490.         
  491.         try:
  492.             self._Thread__started = True
  493.             _active_limbo_lock.acquire()
  494.             _active[_get_ident()] = self
  495.             del _limbo[self]
  496.             _active_limbo_lock.release()
  497.             if __debug__:
  498.                 self._note('%s.__bootstrap(): thread started', self)
  499.             
  500.             if _trace_hook:
  501.                 self._note('%s.__bootstrap(): registering trace hook', self)
  502.                 _sys.settrace(_trace_hook)
  503.             
  504.             if _profile_hook:
  505.                 self._note('%s.__bootstrap(): registering profile hook', self)
  506.                 _sys.setprofile(_profile_hook)
  507.             
  508.             
  509.             try:
  510.                 self.run()
  511.             except SystemExit:
  512.                 if __debug__:
  513.                     self._note('%s.__bootstrap(): raised SystemExit', self)
  514.                 
  515.             except:
  516.                 __debug__
  517.                 if __debug__:
  518.                     self._note('%s.__bootstrap(): unhandled exception', self)
  519.                 
  520.                 s = _StringIO()
  521.                 _print_exc(file = s)
  522.                 _sys.stderr.write('Exception in thread %s:\n%s\n' % (self.getName(), s.getvalue()))
  523.  
  524.             if __debug__:
  525.                 self._note('%s.__bootstrap(): normal return', self)
  526.         finally:
  527.             self._Thread__stop()
  528.             
  529.             try:
  530.                 self._Thread__delete()
  531.             except:
  532.                 pass
  533.  
  534.  
  535.  
  536.     
  537.     def __stop(self):
  538.         self._Thread__block.acquire()
  539.         self._Thread__stopped = True
  540.         self._Thread__block.notifyAll()
  541.         self._Thread__block.release()
  542.  
  543.     
  544.     def __delete(self):
  545.         _active_limbo_lock.acquire()
  546.         del _active[_get_ident()]
  547.         _active_limbo_lock.release()
  548.  
  549.     
  550.     def join(self, timeout = None):
  551.         if not self._Thread__initialized:
  552.             raise AssertionError, 'Thread.__init__() not called'
  553.         if not self._Thread__started:
  554.             raise AssertionError, 'cannot join thread before it is started'
  555.         if not self is not currentThread():
  556.             raise AssertionError, 'cannot join current thread'
  557.         if __debug__:
  558.             if not (self._Thread__stopped):
  559.                 self._note('%s.join(): waiting until thread stops', self)
  560.             
  561.         
  562.         self._Thread__block.acquire()
  563.         if timeout is None:
  564.             while not (self._Thread__stopped):
  565.                 self._Thread__block.wait()
  566.             if __debug__:
  567.                 self._note('%s.join(): thread stopped', self)
  568.             
  569.         else:
  570.             deadline = _time() + timeout
  571.             while not (self._Thread__stopped):
  572.                 delay = deadline - _time()
  573.                 if delay <= 0:
  574.                     if __debug__:
  575.                         self._note('%s.join(): timed out', self)
  576.                     
  577.                     break
  578.                 
  579.                 self._Thread__block.wait(delay)
  580.             if __debug__:
  581.                 self._note('%s.join(): thread stopped', self)
  582.             
  583.         self._Thread__block.release()
  584.  
  585.     
  586.     def getName(self):
  587.         if not self._Thread__initialized:
  588.             raise AssertionError, 'Thread.__init__() not called'
  589.         return self._Thread__name
  590.  
  591.     
  592.     def setName(self, name):
  593.         if not self._Thread__initialized:
  594.             raise AssertionError, 'Thread.__init__() not called'
  595.         self._Thread__name = str(name)
  596.  
  597.     
  598.     def isAlive(self):
  599.         if not self._Thread__initialized:
  600.             raise AssertionError, 'Thread.__init__() not called'
  601.         if self._Thread__started:
  602.             pass
  603.         return not (self._Thread__stopped)
  604.  
  605.     
  606.     def isDaemon(self):
  607.         if not self._Thread__initialized:
  608.             raise AssertionError, 'Thread.__init__() not called'
  609.         return self._Thread__daemonic
  610.  
  611.     
  612.     def setDaemon(self, daemonic):
  613.         if not self._Thread__initialized:
  614.             raise AssertionError, 'Thread.__init__() not called'
  615.         if not not (self._Thread__started):
  616.             raise AssertionError, 'cannot set daemon status of active thread'
  617.         self._Thread__daemonic = daemonic
  618.  
  619.  
  620.  
  621. def Timer(*args, **kwargs):
  622.     return _Timer(*args, **kwargs)
  623.  
  624.  
  625. class _Timer(Thread):
  626.     """Call a function after a specified number of seconds:
  627.  
  628.     t = Timer(30.0, f, args=[], kwargs={})
  629.     t.start()
  630.     t.cancel() # stop the timer's action if it's still waiting
  631.     """
  632.     
  633.     def __init__(self, interval, function, args = [], kwargs = { }):
  634.         Thread.__init__(self)
  635.         self.interval = interval
  636.         self.function = function
  637.         self.args = args
  638.         self.kwargs = kwargs
  639.         self.finished = Event()
  640.  
  641.     
  642.     def cancel(self):
  643.         """Stop the timer if it hasn't finished yet"""
  644.         self.finished.set()
  645.  
  646.     
  647.     def run(self):
  648.         self.finished.wait(self.interval)
  649.         if not self.finished.isSet():
  650.             self.function(*self.args, **self.kwargs)
  651.         
  652.         self.finished.set()
  653.  
  654.  
  655.  
  656. class _MainThread(Thread):
  657.     
  658.     def __init__(self):
  659.         Thread.__init__(self, name = 'MainThread')
  660.         self._Thread__started = True
  661.         _active_limbo_lock.acquire()
  662.         _active[_get_ident()] = self
  663.         _active_limbo_lock.release()
  664.         import atexit
  665.         atexit.register(self._MainThread__exitfunc)
  666.  
  667.     
  668.     def _set_daemon(self):
  669.         return False
  670.  
  671.     
  672.     def _MainThread__exitfunc(self):
  673.         self._Thread__stop()
  674.         t = _pickSomeNonDaemonThread()
  675.         if t:
  676.             if __debug__:
  677.                 self._note('%s: waiting for other threads', self)
  678.             
  679.         
  680.         while t:
  681.             t.join()
  682.             t = _pickSomeNonDaemonThread()
  683.         if __debug__:
  684.             self._note('%s: exiting', self)
  685.         
  686.         self._Thread__delete()
  687.  
  688.  
  689.  
  690. def _pickSomeNonDaemonThread():
  691.     for t in enumerate():
  692.         if not t.isDaemon() and t.isAlive():
  693.             return t
  694.             continue
  695.     
  696.     return None
  697.  
  698.  
  699. class _DummyThread(Thread):
  700.     
  701.     def __init__(self):
  702.         Thread.__init__(self, name = _newname('Dummy-%d'))
  703.         self._Thread__started = True
  704.         _active_limbo_lock.acquire()
  705.         _active[_get_ident()] = self
  706.         _active_limbo_lock.release()
  707.  
  708.     
  709.     def _set_daemon(self):
  710.         return True
  711.  
  712.     
  713.     def join(self, timeout = None):
  714.         if not False:
  715.             raise AssertionError, 'cannot join a dummy thread'
  716.  
  717.  
  718.  
  719. def currentThread():
  720.     
  721.     try:
  722.         return _active[_get_ident()]
  723.     except KeyError:
  724.         return _DummyThread()
  725.  
  726.  
  727.  
  728. def activeCount():
  729.     _active_limbo_lock.acquire()
  730.     count = len(_active) + len(_limbo)
  731.     _active_limbo_lock.release()
  732.     return count
  733.  
  734.  
  735. def enumerate():
  736.     _active_limbo_lock.acquire()
  737.     active = _active.values() + _limbo.values()
  738.     _active_limbo_lock.release()
  739.     return active
  740.  
  741. _MainThread()
  742.  
  743. def _test():
  744.     
  745.     class BoundedQueue(_Verbose):
  746.         
  747.         def __init__(self, limit):
  748.             _Verbose.__init__(self)
  749.             self.mon = RLock()
  750.             self.rc = Condition(self.mon)
  751.             self.wc = Condition(self.mon)
  752.             self.limit = limit
  753.             self.queue = []
  754.  
  755.         
  756.         def put(self, item):
  757.             self.mon.acquire()
  758.             while len(self.queue) >= self.limit:
  759.                 self._note('put(%s): queue full', item)
  760.                 self.wc.wait()
  761.             self.queue.append(item)
  762.             self._note('put(%s): appended, length now %d', item, len(self.queue))
  763.             self.rc.notify()
  764.             self.mon.release()
  765.  
  766.         
  767.         def get(self):
  768.             self.mon.acquire()
  769.             while not (self.queue):
  770.                 self._note('get(): queue empty')
  771.                 self.rc.wait()
  772.             item = self.queue.pop(0)
  773.             self._note('get(): got %s, %d left', item, len(self.queue))
  774.             self.wc.notify()
  775.             self.mon.release()
  776.             return item
  777.  
  778.  
  779.     
  780.     class ProducerThread(Thread):
  781.         
  782.         def __init__(self, queue, quota):
  783.             Thread.__init__(self, name = 'Producer')
  784.             self.queue = queue
  785.             self.quota = quota
  786.  
  787.         
  788.         def run(self):
  789.             random = random
  790.             import random
  791.             counter = 0
  792.             while counter < self.quota:
  793.                 counter = counter + 1
  794.                 self.queue.put('%s.%d' % (self.getName(), counter))
  795.                 _sleep(random() * 1.0000000000000001e-05)
  796.  
  797.  
  798.     
  799.     class ConsumerThread(Thread):
  800.         
  801.         def __init__(self, queue, count):
  802.             Thread.__init__(self, name = 'Consumer')
  803.             self.queue = queue
  804.             self.count = count
  805.  
  806.         
  807.         def run(self):
  808.             while self.count > 0:
  809.                 item = self.queue.get()
  810.                 print item
  811.                 self.count = self.count - 1
  812.  
  813.  
  814.     NP = 3
  815.     QL = 4
  816.     NI = 5
  817.     Q = BoundedQueue(QL)
  818.     P = []
  819.     for i in range(NP):
  820.         t = ProducerThread(Q, NI)
  821.         t.setName('Producer-%d' % (i + 1))
  822.         P.append(t)
  823.     
  824.     C = ConsumerThread(Q, NI * NP)
  825.     for t in P:
  826.         t.start()
  827.         _sleep(9.9999999999999995e-07)
  828.     
  829.     C.start()
  830.     for t in P:
  831.         t.join()
  832.     
  833.     C.join()
  834.  
  835. if __name__ == '__main__':
  836.     _test()
  837.  
  838.